using System;
using Spring.Context;
using Spring.Context.Support;

namespace SpringSeries.Core.EventWiring
{
    public sealed class Program
    {
        [STAThread]
        public static void Main()
        {

            IApplicationContext _ctx = ContextRegistry.GetContext();

            EventPublisher _publisher = (EventPublisher)_ctx.GetObject("MyEventPublisher");
            // registro nel context gli eventi dell'oggetto EventPublisher
            _ctx.PublishEvents(_publisher);

            EventSubscriber _subscriber1 = (EventSubscriber)_ctx.GetObject("MyEventSubscriber");
            EventSubscriber _subscriber2 = (EventSubscriber)_ctx.GetObject("MyEventSubscriber");
            // registro nel context che il subscriber1 sottoscrive gli eventi dell'oggetto EventPublisher
            _ctx.Subscribe(_subscriber1, typeof(EventPublisher));

            Console.WriteLine("Publisher name: " + _publisher.PublisherName);
            
            Console.WriteLine("Subscriber 1 Event Handled: " + _subscriber1.EventHandled);
            Console.WriteLine("Subscriber 2 Event Handled: " + _subscriber2.EventHandled);

            // raises a publisher event...
            _publisher.RaiseSimpleEvent();

            Console.WriteLine("Subscriber 1 Event Handled: " + _subscriber1.EventHandled);
            Console.WriteLine("Subscriber 2 Event Handled: " + _subscriber2.EventHandled);

            Console.Read();
        }
    }
}